package ar.com.gals.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.com.gals.configuration.Configuration;
import ar.com.gals.constants.Page;
import ar.com.gals.error.GalsException;
import ar.com.gals.service.JazzService;


@Controller
@RequestMapping(value = "/Edicion/ListaPrecios")
public class ListaPreciosController extends AbstractController {

	@PreAuthorize("hasRole('ACLPRE')")
	@RequestMapping(method = RequestMethod.GET)
    public String listaDePrecios(Model model) {

		model.addAttribute("action", "M");
		
		return Page.EDICION_LISTA_PRECIOS;
    }
	
	
	@PreAuthorize("hasRole('ACLPRE')")
	@RequestMapping(value = "/Actualizar", method = RequestMethod.POST)
    public @ResponseBody String actualizarListaDePrecios(HttpServletRequest request, HttpServletResponse response) {
	
		try
		{
			String archivo = request.getParameter("archivo").trim();
			
			JazzService service = new JazzService();			
			service.procesarArticulos(Configuration.getInstance().getListaPreciosFolder() + archivo);
			
			JSONObject json = new JSONObject();
			json.put("error", "");
			return json.toString();
		}
		catch(GalsException ex) {
			JSONObject json = new JSONObject();
			json.put("error", ex.getErrors().get(0).getDescripcion());
			return json.toString();
		}
		catch(Exception ex2) {
			JSONObject json = new JSONObject();
			json.put("error", ex2.getMessage());
			return json.toString();
		}
	}
}
